import CommonUtils from "../CommonUtils";
import BButton from "./BButton";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Popup extends cc.Component {

    @property(cc.Node)
    bg: cc.Node = null;

    @property(cc.Node)
    dialog: cc.Node = null;

    @property(cc.Node)
    btnClose: cc.Node = null;

    @property(cc.Integer)
    bgOpacity: number = 100;

    @property(cc.Boolean)
    destroyByClose: boolean = true;

    onClose: Function;

    onDestroy() {
        if (this.destroyByClose) {
            if (this.onClose) {
                this.onClose();
            }
        }
    }

    onDisable(){
        if (!this.destroyByClose) {
            if (this.onClose) {
                this.onClose();
            }
        }
    }

    onLoad() {
        CommonUtils.setTouchMask(this.bg);

        this.bg.opacity = 0;
        cc.tween(this.bg).to(0.2, { opacity: this.bgOpacity }).start();

        this.dialog.scale = 0.5;
        cc.tween(this.dialog).to(0.2, { scale: 1 }, { easing: 'backOut' }).start();

        BButton.addButton(this.btnClose).setTouchUpEvent(() => {
            if (this.destroyByClose) {
                this.node.destroy();
            } else {
                this.node.active = false;
            }
        })

        if (this.bg == this.btnClose) {
            this.btnClose.getComponent(BButton).scaleRatio = 1;
        }
    }
}
